package org.blame.controller;

import org.springframework.ai.chat.messages.SystemMessage;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 何彦声
 * Date: 2025-08-27
 * Time:
 */

@RequestMapping("/ai")
@RestController
public class DeepSeekController {

    @Autowired
    private OpenAiChatModel openAiChatModel;
    @RequestMapping("/chat")
    public String chat(String message){
        return openAiChatModel.call(message);
    }

    @RequestMapping("/chatByPrompt")
    public String chatByPrompt(String message){
        Prompt prompt=new Prompt(message);
        ChatResponse call = openAiChatModel.call(prompt);
        return call.getResult().getOutput().getText();
    }

    @RequestMapping("/roleChat")
    public String roleChat(String message){
        SystemMessage systemMessage=new SystemMessage("你的名字叫小瑞，你是一个智能的电脑管家助手");
        UserMessage userMessage=new UserMessage(message);
        Prompt prompt=new Prompt(systemMessage,userMessage);
        ChatResponse call = openAiChatModel.call(prompt);
        return call.getResult().getOutput().getText();
    }

    @RequestMapping(value = "/streamChat",produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> streamChat(String message){
        Prompt prompt=new Prompt(message);
        return openAiChatModel.stream(prompt).map(x->x.getResults().get(0).getOutput().getText());
    }
}
